package com.cefa.edu.dao;

import com.cefa.edu.model.Course;
import com.cefa.edu.model.StudentStageData;
import com.cefa.edu.pojo.Grade;
import com.cefa.edu.pojo.OrganRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author huangzehui
 * @date 18-8-10 上午11:28
 */




@Mapper
public interface  GradeMapper {

    /**
     * @aa 根据年级id 获取对应的年级数据
     * @aa add by chaijian 2018-08-14
     */
    @Results(id = "gradeMapById", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "gradeName", column = "grade_name"),
            @Result(property = "gradeDate", column = "grade_date"),
            @Result(property = "sort", column = "sort"),
            @Result(property = "schoolId", column = "school_id"),
            @Result(property = "count", column = "count"),
            @Result(property = "type", column = "status"),
            @Result(property = "state", column = "count")
    })
    @Select({"select g.* from cefa_edu_grade g where g.state=0 and g.id=#{id}"})
    Grade getGradeById(Integer id);

    /**
     * @aa 添加年级数据
     * @aa add by chaijian
     * @aa 2018-08-10
     */
  //  @Insert({"INSERT into cefa_edu_grade(grade_name,grade_date,sort,school_id,type)values ${values}"})
    //int saveGrades(@Param("values") String values);

    @Insert({"INSERT into cefa_edu_grade(grade_name,grade_date,sort,school_id,type)" +
            "values(#{gradeName},#{gradeDate},#{sort},#{schoolId},#{type})"})
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int saveGrades(Grade grade);

    @Select("select eg.id,eg.sort,eg.type from cefa_edu_grade eg left join cefa_edu_organ_relation eor on eg.id= eor.grade_id where eg.state=0 and eor.state=0 and eor.phase_id=#{phaseId} group by eor.grade_id,eg.sort")
    @Results(id = "gradeMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "gradeName", column = "grade_name"),
            @Result(property = "gradeDate", column = "grade_date"),
            @Result(property = "sort", column = "sort"),
            @Result(property = "schoolId", column = "school_id"),
            @Result(property = "count", column = "count"),
            @Result(property = "type", column = "type"),
            @Result(property = "state", column = "state")
    })
    List<Grade> findGradeListByPhaseId(@Param("phaseId") Integer phaseId);

    @Update({ "update cefa_edu_grade set grade_name = #{gradeName},sort=sort+1 where id=#{id}" })
    int updateGradeById(@Param("id") Integer id,@Param("gradeName") String gradeName);

    @Insert({ "insert into cefa_edu_grade(grade_name,grade_date,sort,school_id,count,type,state) values(#{gradeName},#{gradeDate},#{sort},#{schoolId},#{count},#{type},#{state})" })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertGrade(Grade grade);


    @Select("select grade_name,grade_date,sort,school_id,count,type,state from cefa_edu_grade where id=#{id}")
    @ResultMap(value="gradeMapById")
    Grade findGradeById(@Param("id") Integer id);

    @Select({"select id,grade_name,grade_date,sort,school_id,count,type,state from cefa_edu_grade  where state=0 and id=#{id}"})
    @ResultMap(value="gradeMapById")
    Grade getGradeById2(@Param("id")Integer id);
}
